package com.example.w_library.utils

import android.app.Activity
import android.app.Application
import android.os.Bundle
import java.lang.ref.WeakReference


/**
 * Activity管理工具
 */
class ActivityManager private constructor(){

    //弱引用存放activist有对象
    private val activityRefs = ArrayList<WeakReference<Activity>>()
    //应用前后台切换监听
    private val frontBackCallbacks = ArrayList<FrontBackCallback>()
    //记录处于前台的activity数量
    private var activityStartCount = 0
    private var fount = true

    //设置加载模式为线程安全,实现单例模式
    companion object{
        val instances:ActivityManager by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            ActivityManager()
        }
    }

    /**
     * 初始化
     */
    fun init(application: Application){
        application.registerActivityLifecycleCallbacks(InnerRegisterActivityLifecycleCallbacks())
    }


    /**
     * 发生变化通知
     */
    private fun onFountBackCHanged(b: Boolean) {
        for (frontBackCallback in frontBackCallbacks) {
            frontBackCallback.onChanged(b)
        }
    }

    /**
     * 获取栈顶activity
     */
    val topActivity : Activity? get() {

        return if (activityStartCount <= 0){
            null
        }else{
            activityRefs.get(activityRefs.size -1).get()
        }
    }


    /**
     * 添加监听
     */
    public fun addFountBackCallback(callback : FrontBackCallback){
        frontBackCallbacks.add(callback)
    }

    /**
     * 移除监听
     */
    public fun removeFountBackCallback(callback : FrontBackCallback){
        frontBackCallbacks.remove(callback)
    }

    interface FrontBackCallback{
        fun onChanged(fount : Boolean)
    }

    inner class InnerRegisterActivityLifecycleCallbacks : Application.ActivityLifecycleCallbacks{
        override fun onActivityPaused(activity: Activity) {

        }

        override fun onActivityStarted(activity: Activity) {
            activityStartCount++
            //activityStartCount > 0 说明应用处在可见状态,也就是前台
            //!fount 之前不是在前台 fount == false
            if (!fount && activityStartCount > 0){
                fount = true
                onFountBackCHanged(true)
            }

        }

        override fun onActivityDestroyed(activity: Activity) {
            for (activityRef in activityRefs) {
                if (activityRef.get() == activity){
                    activityRefs.remove(activityRef)
                    break
                }
            }
        }

        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
        }

        override fun onActivityStopped(activity: Activity) {
            activityStartCount--
            if (fount && activityStartCount <= 0){
                fount = false
                onFountBackCHanged(false)
            }
        }

        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            activityRefs.add(WeakReference(activity))
        }

        override fun onActivityResumed(activity: Activity) {
        }

    }

}